<template>
  <div class="home-category">
    <!-- 判断有没有获取到数据 -->
    <template v-if="newList.length >0">
    <ul class="menu">
      <li
        v-for="item in newList"
        :key="item.id"
        @mouseenter="mouseEnter(item.id)">
        <RouterLink to="/">{{ item.name }}</RouterLink>
        <template v-for="obj in item.children" :key="obj.id">
          <RouterLink to="/">
            {{ obj.name }}
          </RouterLink>
        </template>
        <!-- 弹层 -->
        <div class="layer">
          <h4>分类推荐 <small>根据您的购买或浏览记录推荐</small></h4>
          <!-- 判断categoryList && categoryList.goods && categoryList.goods.length这三者都为true的情况下,才渲染页面,如果没有判断,会报错 -->
          <ul
            v-if="categoryList && categoryList.goods && categoryList.goods.length">
            <li v-for="item in categoryList.goods" :key="item.id">
              <RouterLink :to="`/goods/${item.id}`">
                <img :src="item.picture" alt="" />
                <div class="info">
                  <p class="name ellipsis-2">{{ item.name }}</p>
                  <p class="desc ellipsis">{{ item.desc }}</p>
                  <p class="price"><i>¥</i>{{ item.price }}</p>
                </div>
              </RouterLink>
            </li>
          </ul>
        </div>
      </li>
    </ul>
    </template>
    <template v-else>
      <ul class="menu">
       <li v-for="i in 9" :key="i">
          <XtxSkeleton
            width="40px"
            height="20px"
            style="margin-right: 5px"
            bg="rgba(255,255,255,0.2)"/>
          <XtxSkeleton
            width="50px"
            height="20px"
            bg="rgba(255,255,255,0.2)"
            style="margin-right: 5px"
          />
          <XtxSkeleton width="50px" height="20px" bg="rgba(255,255,255,0.2)" />
        </li>
        </ul>
    </template>
  </div>
</template>

<script>
export default {
  name: 'HomeCategory',
  data () {
    return {
      categoryId: null
    }
  },
  computed: {
    // 从vuex中获取到渲染页面的数据
    newList () {
      return this.$store.state.category.list.map((item) => {
        return {
          name: item.name,
          id: item.id,
          children: item.children.slice(0, 2)
        }
      })
    },
    // 鼠标移入之后,要根据移入当前的id来渲染所对应的弹层的页面
    categoryList () {
      return this.$store.state.category.list.find(
        item => item.id === this.categoryId
      )
    }
  },
  methods: {
    // 鼠标移入事件
    mouseEnter (id) {
      this.categoryId = id
    }
  }
}
</script>

<style scoped lang='less'>
.home-category {
  width: 250px;
  height: 500px;
  background: rgba(0, 0, 0, 0.8);
  position: relative;
  z-index: 99;
  .menu {
    li {
      padding-left: 40px;
      height: 55px;
      line-height: 55px;
      &:hover {
        background: @xtxColor;
      }
      a {
        margin-right: 4px;
        color: #fff;
        &:first-child {
          font-size: 16px;
        }
      }
      .layer {
        width: 990px;
        height: 500px;
        background: rgba(255, 255, 255, 0.8);
        position: absolute;
        left: 250px;
        top: 0;
        display: none;
        padding: 0 15px;
        h4 {
          font-size: 20px;
          font-weight: normal;
          line-height: 80px;
          small {
            font-size: 16px;
            color: #666;
          }
        }
        ul {
          display: flex;
          flex-wrap: wrap;
          li {
            width: 310px;
            height: 120px;
            margin-right: 15px;
            margin-bottom: 15px;
            border: 1px solid #eee;
            border-radius: 4px;
            background: #fff;
            &:nth-child(3n) {
              margin-right: 0;
            }
            a {
              display: flex;
              width: 100%;
              height: 100%;
              align-items: center;
              padding: 10px;
              &:hover {
                background: #e3f9f4;
              }
              img {
                width: 95px;
                height: 95px;
              }
              .info {
                padding-left: 10px;
                line-height: 24px;
                overflow: hidden;
                .name {
                  font-size: 16px;
                  color: #666;
                }
                .desc {
                  color: #999;
                }
                .price {
                  font-size: 22px;
                  color: @priceColor;
                  i {
                    font-size: 16px;
                  }
                }
              }
            }
          }
        }
      }
      &:hover {
        .layer {
          display: block;
        }
      }
    }
  }
}
</style>
